package model

import (
	"errors"
	"gorm.io/gorm"
)

type TableFieldsDao interface {
	GetTablesFields(tableName string) (*[]TableFields, error)
}
type defaultTableFieldsDao struct {
	engine *gorm.DB
}

func NewTableFieldsDao(engine *gorm.DB) *defaultTableFieldsDao {
	return &defaultTableFieldsDao{
		engine: engine,
	}
}

func (d defaultTableFieldsDao) GetTablesFields(tableName string) (*[]TableFields, error) {
	// 获取指定表信息
	tableFields := new([]TableFields)
	if tableName == "" {
		return tableFields, errors.New("表名不能为空")
	}
	d.engine.Raw("show FULL COLUMNS from " + tableName + ";").Find(&tableFields)
	return tableFields, nil
}
